import { ZCC_TYPE } from "../../../../typings/types/common"
import { Litigation } from "../../../../typings/types/litigation"
import { getAssistanceUnfreezeApi, getCourtNoticeApi, getEquityFreezeApi, getFinalCaseApi, getJudicialAuctionApi, getOpenCourtNoticeApi, getRegisterApi } from "../api/shesu"
import { formateDigit } from "../../../utils/util"

export namespace LitigationVM {
  export enum Scene {
    none = '',
    openCourtNotice = 'countNotice',//开庭公告
    courtNotice = 'countAnnouncement',//法院公告
    register = 'countRegister',//立案
    finalCase = 'countFinalCase',//终本案件
    restrictingHighConsumption = 'restrictingHighConsumption',//限制高消费
    dishonestExecutor = 'dishonestExecutor',//失信执行人
    bankruptcy = 'bankruptcy',//破产重整企业
    litigationInfo = 'litigationInfo',//涉诉信息
    subordinateDebtor = 'subordinateDebtor',//次债务人
    countEquityFreeze = 'countEquityFreeze',//冻结
    countAssistanceUnfreeze = 'countAssistanceUnfreeze',//解冻
    countAuction = 'countAuction',//司法拍卖
  }

  export interface Result {
    items: Litigation.LitigationListCell[]
    total: number
    hasNextPage: boolean
  }
  //PT: 参数 type; RT :result type 需要返回的数据模型
  export interface ViewModelInterface<PT, RT> {
    loadDataForScene(scene: Scene | string, params: PT, successCallback: (res: RT) => void, finallyCallback: () => void): void

    processLocalDataForScene(scene: Scene | string, local: any | null): RT
  }
}

export class LitigationListViewModel implements LitigationVM.ViewModelInterface<any, LitigationVM.Result> {
  // private titles = ['开庭公告', '法院公告', '立案信息', '终本案件', '限制高消费信息', '失信执行人信息', '破产重整企业']
  constructor() {
  }
  processLocalDataForScene(_scene: LitigationVM.Scene | string, _local: any): LitigationVM.Result {
    throw new Error("Method not implemented.")
  }

  async loadDataForScene(scene: LitigationVM.Scene, params: any, successCallback: (res: LitigationVM.Result) => void, finallyCallback: () => void) {
    wx.showLoading({
      title: ''
    })
    try {
      let tmp: LitigationVM.Result | null = null
      switch (scene) {
        case LitigationVM.Scene.openCourtNotice:
          const res = await getOpenCourtNoticeApi(params)
          if (res.content) {
            console.log(res.content)
            tmp = this.processOpenCourtListDatas(res.content)
          }
          break
        case LitigationVM.Scene.courtNotice:
          const res_c = await getCourtNoticeApi(params)
          if (res_c.content) {
            console.log(res_c.content)
            tmp = this.processCourtListDatas(res_c.content)
          }
          break
        case LitigationVM.Scene.register:
          const res_r = await getRegisterApi(params)
          if (res_r.content) {
            console.log(res_r.content)
            tmp = this.processRegisterListDatas(res_r.content)
          }

          break
        case LitigationVM.Scene.finalCase:
          const res_f = await getFinalCaseApi(params)
          if (res_f.content) {
            console.log(res_f.content)
            tmp = this.processFinalCaseListDatas(res_f.content)
          }

          break
        case LitigationVM.Scene.countEquityFreeze:
          {
            const res_f = await getEquityFreezeApi(params)
            if (res_f.content) {
              console.log(res_f.content)
              tmp = this.processEquityFreezeListDatas(res_f.content)
            }
          }
          break
        case LitigationVM.Scene.countAssistanceUnfreeze:
          {
            const res_f = await getAssistanceUnfreezeApi(params)
            if (res_f.content) {
              console.log(res_f.content)
              tmp = this.processAssistanceUnfreezeListDatas(res_f.content)
            }

          }
          break
        case LitigationVM.Scene.countAuction:
          {
            const res_f = await getJudicialAuctionApi(params)
            if (res_f.content) {
              console.log(res_f.content)
              tmp = this.processJudicialAuctionListDatas(res_f.content)
            }

          }
          break
        case LitigationVM.Scene.bankruptcy:
          break
        case LitigationVM.Scene.dishonestExecutor:
          break
        case LitigationVM.Scene.litigationInfo:
          break
        case LitigationVM.Scene.subordinateDebtor:
          break
        default: break

      }
      if (tmp) {
        successCallback(tmp)
      }

    } finally {
      wx.hideLoading()
      finallyCallback()
    }

  }

  processOpenCourtListDatas(content: ZCC_TYPE.ListResult<Litigation.OpenCourtNoticeDetail>) {

    const cells = content.list.map((item: Litigation.OpenCourtNoticeDetail) => {
      return {
        title: item.caseNo,
        id: item.id,
        rows: [
          { title: '案由：', val: item.caseReason, ishorizontal: true },
          { title: '原告/上诉人：', val: item.prosecutor, ishorizontal: true },
          { title: '被告/被上诉人：', val: item.defendant, ishorizontal: true },
          { title: '法院：', val: item.executeGov, ishorizontal: true },
          { title: '开庭时间：', val: item.lianDate, ishorizontal: true },
        ]
      }
    })

    return this.processListDatas(cells, content.total, content.hasNextPage)
  }

  processCourtListDatas(content: ZCC_TYPE.ListResult<Litigation.CourtNoticeDetail>) {

    const cells = content.list.map((item: Litigation.CourtNoticeDetail) => {
      return {
        id: item.id,
        rows: [
          { title: '上诉人：', val: item.prosecutorList, ishorizontal: true },
          { title: '被上诉人：', val: item.defendantList, ishorizontal: true },
          { title: '公告法院：', val: item.court, ishorizontal: true },
          { title: '更新时间：', val: item.updatedDate, ishorizontal: true },
        ]
      }
    })

    return this.processListDatas(cells, content.total, content.hasNextPage)
  }

  processRegisterListDatas(content: ZCC_TYPE.ListResult<Litigation.RegisterDetail>) {

    const cells = content.list.map((item: Litigation.RegisterDetail) => {
      return {
        title: item.caseNo,
        id: item.registerId,
        rows: [
          { title: '公诉人/原告/上诉人/申请人：', val: item.prosecutor },
          { title: '被告人/被告/被上诉人/被申请人：', val: item.appellee },
          { title: '立案时间：', val: item.punishDate, ishorizontal: true },
        ]
      }
    })

    return this.processListDatas(cells, content.total, content.hasNextPage)
  }
  processFinalCaseListDatas(content: ZCC_TYPE.ListResult<Litigation.FinalCaseDetail>) {

    const cells = content.list.map((item: Litigation.FinalCaseDetail) => {
      return {
        title: item.caseNo,
        id: item.id,
        rows: [
          { title: '被执行人：', val: item.iname, ishorizontal: true },
          { title: '未履行金额：', val: item.unperfMoney, style: 'color:#F04040', valsubfix: '元', ishorizontal: true },
          { title: '执行标的：', val: item.execMoney, style: 'color:#F04040', valsubfix: '元', ishorizontal: true },
          { title: '执行法院：', val: item.courtName, ishorizontal: true },
          { title: '立案时间：', val: item.regDate, ishorizontal: true },
          { title: '终本时间：', val: item.finalDate, ishorizontal: true },
        ]
      }
    })

    return this.processListDatas(cells, content.total, content.hasNextPage)
  }
  processEquityFreezeListDatas(content: ZCC_TYPE.ListResult<Litigation.EquityFreeze>) {

    const cells = content.list.map((item: Litigation.EquityFreeze) => {
      return {
        title: item.executionDocNum || '-',
        id: item.id,
        orign: item,
        rows: [
          { title: '企业名称：', val: item.companyName, ishorizontal: true },
          { title: '执行事项：', val: item.executionMatters, ishorizontal: true },
          { title: '冻结开始时间：', val: item.freezeStartDate, ishorizontal: true },
          { title: '冻结结束时间：', val: item.freezeEndDate, ishorizontal: true },
          { title: '公示时间：', val: item.publicDate, ishorizontal: true },
        ]
      }
    })

    return this.processListDatas(cells, content.total, content.hasNextPage)
  }
  processAssistanceUnfreezeListDatas(content: ZCC_TYPE.ListResult<Litigation.AssistanceUnfreeze>) {

    const cells = content.list.map((item: Litigation.AssistanceUnfreeze) => {
      return {
        title: item.executionDocNum || '-',
        id: item.id,
        orign: item,
        rows: [
          { title: '企业名称：', val: item.companyName, ishorizontal: true },
          { title: '执行事项：', val: item.executionMatters, ishorizontal: true },
          { title: '冻结开始时间：', val: item.freezeStartDate, ishorizontal: true },
          { title: '冻结结束时间：', val: item.unFreezeDate, ishorizontal: true },
          { title: '公示时间：', val: item.publicDate, ishorizontal: true },
        ]
      }
    })

    return this.processListDatas(cells, content.total, content.hasNextPage)
  }
  processJudicialAuctionListDatas(content: ZCC_TYPE.ListResult<Litigation.JudicialAuction>) {

    const cells = content.list.map((item: Litigation.JudicialAuction) => {
      return {
        title: item.auctionNotice || '-',
        id: item.id,
        rows: [
          { title: '起拍价格：', val: formateDigit(item.salePrice), style: 'color:#F04040', valsubfix: '元', ishorizontal: true },
          { title: '评估价格：', val: formateDigit(item.appraisalPrice), style: 'color:#F04040', valsubfix: '元', ishorizontal: true },
          { title: '拍卖开始时间：', val: item.auctionStartTime, ishorizontal: true },
          { title: '拍卖结束时间：', val: item.auctionEndTime, ishorizontal: true },
          { title: '成交价格：', val: formateDigit(item.turnoverPrice), style: 'color:#F04040', valsubfix: '元', ishorizontal: true },
          { title: '成交时间：', val: item.tradingDate, ishorizontal: true },
          { title: '执行法院：', val: item.courtName, ishorizontal: true },
          { title: '标的类型：', val: item.markType, ishorizontal: true },
        ]
      }
    })

    return this.processListDatas(cells, content.total, content.hasNextPage)
  }
  processListDatas(items: Litigation.LitigationListCell[], total: number, hasNextPage: boolean) {

    return {
      items: items,
      total: total,
      hasNextPage: hasNextPage
    }
  }

  // titles = ['开庭公告', '法院公告', '立案信息', '终本案件', '限制高消费信息', '失信执行人信息', '破产重整企业']
  getTitle(scene: LitigationVM.Scene) {
    switch (scene) {
      case LitigationVM.Scene.openCourtNotice:
        return '开庭公告'

      case LitigationVM.Scene.courtNotice:
        return '法院公告'

      case LitigationVM.Scene.register:
        return '立案信息'
        break
      case LitigationVM.Scene.finalCase:
        return '终本案件'
      case LitigationVM.Scene.countEquityFreeze:
        return '股权冻结'
      case LitigationVM.Scene.countAssistanceUnfreeze:
        return '解除冻结'
      case LitigationVM.Scene.countAuction:
        return '司法拍卖'

      default: return ''
    }

  }

  getSearchPlaceholder(scene: LitigationVM.Scene) {
    switch (scene) {
      case LitigationVM.Scene.openCourtNotice:
        return '请输入案号/原告/被告'

      case LitigationVM.Scene.courtNotice:
        return '请输入上诉人/被上诉人'

      case LitigationVM.Scene.register:
        return '请输入案号/原告/被告'
        break
      case LitigationVM.Scene.finalCase:
        return '请输入案号/被执行人'
      case LitigationVM.Scene.countEquityFreeze:
      case LitigationVM.Scene.countAssistanceUnfreeze:
        return '请输入执行文书号/企业名称'
      case LitigationVM.Scene.countAuction:
        return '请输入拍卖公告标题'
      default: return ''
    }

  }
}